//
//  MSVisitorView.swift
//  SwiftWeibo
//
//  Created by 牟松 on 16/8/5.
//  Copyright © 2016年 DoNews. All rights reserved.
//

import UIKit

class MSVisitorView: UIView {
    
    
    lazy var registButton: UIButton = UIButton (MS_Button: "注册", titleColor: #colorLiteral(red: 0.9960784314, green: 0.5490196078, blue: 0.1411764706, alpha: 1), selectedTitleColor: #colorLiteral(red: 0.9960784314, green: 0.5490196078, blue: 0.1411764706, alpha: 1), backgroundImageNmae: "common_button_alpha", selectedBackgroundImageName: "common_button_alpha_highlighted")
    
    lazy var loginButton: UIButton = UIButton (MS_Button: "登录", titleColor: #colorLiteral(red: 0.5296475887, green: 0.5296317339, blue: 0.5296407342, alpha: 1), selectedTitleColor: #colorLiteral(red: 0.5296475887, green: 0.5296317339, blue: 0.5296407342, alpha: 1), backgroundImageNmae: "common_button_alpha", selectedBackgroundImageName: "common_button_alpha_highlighted")
    
    lazy var goFocusButton: UIButton = UIButton(MS_Button: "去关注", titleColor: #colorLiteral(red: 0.9960784314, green: 0.5490196078, blue: 0.1411764706, alpha: 1), selectedTitleColor: #colorLiteral(red: 0.9960784314, green: 0.5490196078, blue: 0.1411764706, alpha: 1),  backgroundImageNmae: "common_button_alpha", selectedBackgroundImageName: "common_button_alpha_highlighted")
    
    
    // 决定访客视图内容的字典
    var visitorDetailDic: [String : String]?{
        didSet{
            guard let VCName = visitorDetailDic?["VCName"],
            let smallLabelText = visitorDetailDic?["smallLabelText"]
            else {
                return
            }
            
            smallLabel.text = smallLabelText
            
            if VCName == "MSHomeViewController" {
                registButton.isHidden = true
                loginButton.isHidden = true
                smallIconAmitionStart()
            }
            if VCName == "MSMessageViewController" {
                smallIcon.isHidden = true
                backgroundView.isHidden = true
                goFocusButton.isHidden = true
                houseIcon.image = UIImage(named: "visitordiscover_image_message")
            }
            if VCName == "MSDiscoverViewController" {
                
            }
            if VCName == "MSProfileViewController" {
                houseIcon.isHidden = true
                smallIcon.isHidden = true
                backgroundView.isHidden = true
                goFocusButton.isHidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    // MARK: - 私有控件
    private lazy var smallIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var houseIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var smallLabel: UILabel = UILabel(MS_Label: "关注一些人，回这里看看有什么惊喜", fontSize: 14.0, textColor: #colorLiteral(red: 0.7602152824, green: 0.7601925135, blue: 0.7602053881, alpha: 1))
    
    
    private lazy var backgroundView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    // smallIcon转动动画
    private func smallIconAmitionStart() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        
        anim.isRemovedOnCompletion = false // 动画完成不删除，随view消失一起呗删除
        smallIcon.layer.add(anim, forKey: nil)
        
    }

}

extension MSVisitorView {
    private func setupUI() {
        
        backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.9294117647, blue: 0.9294117647, alpha: 1)
        
        addSubview(smallIcon)
        addSubview(backgroundView)
        addSubview(houseIcon)
        addSubview(smallLabel)
        addSubview(goFocusButton)
        addSubview(registButton)
        addSubview(loginButton)
        
        // 布局
        for subView in subviews {// 取消每一个子视图的自动布局
            subView.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // smallIcon
        addConstraint(NSLayoutConstraint(item: smallIcon, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: smallIcon, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0))
        // 小房子
        addConstraint(NSLayoutConstraint(item: houseIcon, attribute: .centerX, relatedBy: .equal, toItem: smallIcon, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIcon, attribute: .centerY, relatedBy: .equal, toItem: smallIcon, attribute: .centerY, multiplier: 1.0, constant: 0))
        // smallLabel
        addConstraint(NSLayoutConstraint(item: smallLabel, attribute: .centerX, relatedBy: .equal, toItem: smallIcon, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: smallLabel, attribute: .centerY, relatedBy: .equal, toItem: smallIcon, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: smallLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 236))
        // goFocusButton
        addConstraint(NSLayoutConstraint(item: goFocusButton, attribute: .centerX, relatedBy: .equal, toItem: smallLabel, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: goFocusButton, attribute: .centerY, relatedBy: .equal, toItem: smallLabel, attribute: .bottom, multiplier: 1.0, constant: 40))
        addConstraint(NSLayoutConstraint(item: goFocusButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        // backgroundView
        // VFL布局
        let views = ["backgroundView": backgroundView, "goFocusButton": goFocusButton]
        let metrics = ["spacing": 50, "top": 0]
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[backgroundView]-0-|", options: [], metrics: nil, views: views))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(top)-[backgroundView]-(spacing)-[goFocusButton]", options: [], metrics: metrics, views: views))
        // registerButton
        addConstraint(NSLayoutConstraint(item: registButton, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 40))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: .top, relatedBy: .equal, toItem: smallLabel, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        // loginButton
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: registButton, attribute: .top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: -40))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        
        
        
    }
}
